"use strict";
///<reference path="../creator.d.ts"/>
var QM;
(function (QM) {
    /** 位置 */
    var QPoint;
    (function (QPoint) {
        /** 下 */
        QPoint[QPoint["Bottom"] = 1] = "Bottom";
        /** 右 */
        QPoint[QPoint["Right"] = 2] = "Right";
        /** 左 */
        QPoint[QPoint["Left"] = 3] = "Left";
    })(QPoint = QM.QPoint || (QM.QPoint = {}));
    QM.CName = ["Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "One", "Two", "SJoker", "LJoker"];
    QM.CColor = ["Spade", "Heart", "Club", "Square"];
    var Socket = /** @class */ (function () {
        function Socket() {
            this.dial = "ws://182.61.37.167:9501";
            this.websocket = null;
            this.interval = 0;
            this.events = [];
            if (Socket.Instance) {
                return Socket.Instance;
            }
        }
        Socket.prototype.on = function (func, target) {
            this.events.push({ target: target, func: func });
        };
        Socket.prototype.off = function (func, target) {
            var _this = this;
            this.events.forEach(function (item, key) {
                if (item.func == func && item.target == target) {
                    _this.events.splice(key, 1);
                }
            });
        };
        Socket.prototype.connect = function () {
            var _this = this;
            if (this.websocket && this.websocket.readyState == WebSocket.OPEN) {
                return;
            }
            this.websocket = new WebSocket(this.dial);
            this.websocket.onclose = function (ev) {
                _this.onClose(ev);
            };
            this.websocket.onerror = function (ev) {
                _this.onError(ev);
            };
            this.websocket.onopen = function (ev) {
                _this.onOpen(ev);
            };
            this.websocket.onmessage = function (ev) {
                _this.onMessage(ev);
            };
            this.interval = setInterval(function () {
                _this.frame();
            }, 30 * 1000);
        };
        Socket.prototype.frame = function () {
            if (this.websocket && this.websocket.readyState === WebSocket.OPEN) {
                this.websocket.send("");
            }
        };
        Socket.prototype.Send = function (data) {
            if (this.websocket.readyState === WebSocket.OPEN) {
                if (typeof data == "object") {
                    data = JSON.stringify(data);
                }
                this.websocket.send(data);
            }
            else {
                console.log("WebSocket 连接错误");
            }
        };
        Socket.prototype.onOpen = function (ev) {
            console.log("连接打开");
            this.bubbleUp(ev);
        };
        Socket.prototype.onMessage = function (ev) {
            if (ev.data) {
                var sd = JSON.parse(ev.data);
                var e = {
                    type: ev.type,
                    cmd: sd.cmd,
                    data: sd.data
                };
                this.bubbleUp(e);
            }
        };
        Socket.prototype.onClose = function (ev) {
            clearInterval(this.interval);
            console.log("连接被关闭");
            //cc.director.loadScene("hall"); 
            //this.bubbleUp(ev);
        };
        Socket.prototype.onError = function (ev) {
            clearInterval(this.interval);
            console.log("连接错误");
            //this.bubbleUp(ev);
        };
        Socket.prototype.bubbleUp = function (ev) {
            this.events.forEach(function (item) {
                item.func.call(item.target, ev);
            });
        };
        Socket.Instance = new Socket();
        return Socket;
    }());
    QM.Socket = Socket;
    /**
     * 房间管理
     */
    var Room = /** @class */ (function () {
        function Room() {
            this._roomId = 0;
            this._landLord = 0;
            this.characters = [];
            if (Room.Instance) {
                return Room.Instance;
            }
        }
        Object.defineProperty(Room.prototype, "roomId", {
            get: function () {
                return this._roomId;
            },
            set: function (rid) {
                if (!this._roomId) {
                    this._roomId = rid;
                }
                else {
                    console.log("出错,房间号已设置");
                }
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(Room.prototype, "landLord", {
            get: function () {
                return this._landLord;
            },
            set: function (uid) {
                this._landLord = uid;
            },
            enumerable: true,
            configurable: true
        });
        Room.prototype.addCharacter = function (ch) {
            if (this.length >= 3) {
                console.log("房间已满,无法加入");
                return false;
            }
            if (this.in_Character(ch)) {
                return;
            }
            if (ch.uid == QM.User.uid) {
                ch.point = QPoint.Bottom;
            }
            else {
                if (!this.getCharacterForPoint(QPoint.Left)) {
                    ch.point = QPoint.Left;
                }
                else if (!this.getCharacterForPoint(QPoint.Right)) {
                    ch.point = QPoint.Right;
                }
            }
            this.characters.push(ch);
        };
        Room.prototype.getCharacterForPoint = function (point) {
            for (var i = 0; i < this.characters.length; i++) {
                if (this.characters[i].point == point) {
                    return this.characters[i];
                }
            }
            return null;
        };
        Room.prototype.getCharacterForUid = function (uid) {
            for (var i = 0; i < this.characters.length; i++) {
                if (this.characters[i].uid == uid) {
                    return this.characters[i];
                }
            }
            return null;
        };
        Room.prototype.in_Character = function (ch) {
            for (var i = 0; i < this.length; i++) {
                if (this.characters[i].uid == ch.uid) {
                    return true;
                }
            }
            return false;
        };
        Room.prototype.removeCharacter = function (ch) {
            for (var i = 0; i < this.length; i++) {
                if (this.characters[i].uid == ch.uid) {
                    return this.characters.splice(i, 1)[0];
                }
            }
            return null;
        };
        Room.prototype.getCharacters = function () {
            return this.characters;
        };
        Room.prototype.clear = function () {
            this.roomId = 0;
            this.landLord = 0;
            this.characters = [];
        };
        Object.defineProperty(Room.prototype, "length", {
            get: function () {
                return this.characters.length;
            },
            enumerable: true,
            configurable: true
        });
        Room.Instance = new Room();
        return Room;
    }());
    QM.Room = Room;
    var JSManager = /** @class */ (function () {
        function JSManager() {
            this.objs = {};
        }
        JSManager.prototype.setClass = function (fun, obj) {
            var key = fun["name"];
            this.objs[key] = obj;
        };
        JSManager.prototype.getClass = function (fun) {
            var key = fun["name"];
            return this.objs[key];
        };
        JSManager.instance = new JSManager();
        return JSManager;
    }());
    QM.JSManager = JSManager;
})(QM || (QM = {}));
window.QM = QM;
